﻿#include "FITKAbaqusINPReader.h"
#include "FITKAbaqusAdaptorCase.h"
#include "InpKeyWordsChecker.h"
#include "InpReadInformationMapper.h"
#include "FITK_Kernel/FITKAdaptor/FITKIOAdaptorFactory.h"
#include "FITK_Kernel/FITKAppFramework/FITKMessage.h"
#include "FITK_Component/FITKAbaqusData/FITKDataCase.h"

//命名空间
namespace IO
{

    FITKAbaqusINPReader::FITKAbaqusINPReader(INPReadLevel level)
        :_readLevel(level)
    {
        _keyWordsChecker = new InpKeyWordsChecker(this);
    }

    FITKAbaqusINPReader::~FITKAbaqusINPReader()
    {
        if (_keyWordsChecker != nullptr) delete _keyWordsChecker;
    }

    void FITKAbaqusINPReader::run()
    {
        //加载文件
        bool ok = this->loadFile();
        if (!ok) return;
        //强制类型转换
        AbaqusData::FITKDataCase *caseData =
            AbaqusData::FITKDataCase::SafeDownCastFITKDataCase(_data);
        if (caseData == nullptr) return;

        //section与材料映射
        if (_informationReadInpMapper != nullptr)
            delete _informationReadInpMapper;
        _informationReadInpMapper = new InpReadInformationMapper(caseData);

        auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorCase>("INP", caseData);
        if (adaptor == nullptr) return;
        adaptor->setDataObject(caseData);
        adaptor->setFileReader(this);
        if (_resultMark != nullptr)
            *_resultMark = adaptor->adaptR();
        else
            adaptor->adaptR();
        delete adaptor;

        //映射器并释放
        if (_informationReadInpMapper)
        {
            //开始映射
            _informationReadInpMapper->map();
            delete _informationReadInpMapper;
        }

        //读取完成
        this->sendCurrentPercent(100);
    }

    QString FITKAbaqusINPReader::readLine()
    {
        QString line;
        while (!atEnd())
        {
            line = FITKAbstractTextReader::readLine().simplified();
            // 空行跳过
            if (_isSkipBlankLine && line.isEmpty())continue;
            // 注释行跳过
            if (line.startsWith("**") && line.size() == 2) continue;
            break;
        }
        line.remove('"');
        return line;
    }

    void FITKAbaqusINPReader::backLine()
    {
        FITKAbstractTextReader::backLine();
        if (_isSkipBlankLine && this->getCurrentLine().isEmpty() && this->getCurrentLineNum() > 1)
            this->backLine();
    }

    void FITKAbaqusINPReader::setSkipBlankLine(bool isSkip)
    {
        _isSkipBlankLine = isSkip;
    }

    //QString FITKAbaqusINPReader::previousLine()
    //{
    //    QString line;
    //    while (!atEnd())
    //    {
    //        line = FITKAbstractTextReader::readLine().simplified();
    //        if (line.isEmpty())continue;
    //        // 注释行跳过
    //        if (line.startsWith("**") && line.size() == 2) continue;
    //        break;
    //    }
    //    return line;
    //}

    InpReadInformationMapper* FITKAbaqusINPReader::getReadInpInformationMapper()
    {
        return _informationReadInpMapper;
    }

    INPReadLevel FITKAbaqusINPReader::getINPReadLevel()
    {
        return _readLevel;
    }

    bool FITKAbaqusINPReader::checkKeyWords(const QString& header, const QStringList& keys, const QHash<QString, QString>& keyValues)
    {
        if (_keyWordsChecker)
            return _keyWordsChecker->check(header, keys, keyValues);
        return false;
    }
    
    void FITKAbaqusINPReader::consoleMessage(int level, const QString& str)
    {
        //判断消息等级
        switch (level)
        {
        case 1:AppFrame::FITKMessageNormal(str); break;
        case 2: AppFrame::FITKMessageWarning(str); break;
        case 3:AppFrame::FITKMessageError(str); break;
        default: AppFrame::FITKMessageError(str); break;
        }
    }
}


